package com.hzdl.flower.controller;

import com.hzdl.flower.bean.Cart;
import com.hzdl.flower.bean.Flower;
import com.hzdl.flower.bean.User;
import com.hzdl.flower.mapper.UserMapper;
import com.hzdl.flower.service.CartService;
import com.hzdl.flower.service.FlowerService;
import com.hzdl.flower.service.UserService;
import com.hzdl.flower.util.VerifyCodeUtils;
import com.hzdl.flower.vo.CartVo;
import com.hzdl.flower.vo.FlowerVo;
import com.hzdl.flower.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("user")


public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private CartService CartService;
    @Autowired
    private FlowerService flowerService;

    @RequestMapping("userLoginInit")
    public String userLoginInit() {

        return "user/ulogin";
    }

    @RequestMapping("userLogin")
    public String userLogin(Model model, HttpSession session, HttpServletRequest req, String userPwd, String userLoginId) {
        UserVo userVo = new UserVo();
        userVo.setUserLoginId(userLoginId);
        userVo.setUserPwd(userPwd);
        User user = userService.login(userVo);

        if (user == null) {

            model.addAttribute("msg", "登录失败");

        }



        //把用户对象存入session
        req.getSession().invalidate();
        HttpSession s1 = req.getSession();

        s1.setAttribute("user", user);
        s1.setMaxInactiveInterval(60 * 30);

        return "shop/index";
    }

    @RequestMapping("userRegisterInit")
    private String userRegisterInit(UserVo userVo) {

        return "user/uregister";

    }

    @RequestMapping("userRegister")
    private String userRegister(UserVo userVo, Model model) {
        int row = userService.register(userVo);
        if (row == 0) {

            model.addAttribute("msg", "注册失败");

        }
        return "shop/index";

    }

    //生成验证码
    @RequestMapping("/autoyzm")
    public void shenchenYzm(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String autoYzm = VerifyCodeUtils.generateVerifyCode(4);
        //验证码字符串Session保存
        req.getSession().setAttribute("SESSION_AUTO_YZM", autoYzm);
        VerifyCodeUtils.outputImage(100, 30, resp.getOutputStream(), autoYzm);
    }

    //检查验证码
    @RequestMapping("/chkyzm")
    public void chkyzm(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 获取验证码
        String yzm = req.getParameter("yzmKey");
        // 获取session中的验证码
        String sessionYzm = (String) req.getSession().getAttribute("SESSION_AUTO_YZM");
        int i = 0;
        //忽略大小写检查
        if (!yzm.equalsIgnoreCase(sessionYzm)) {
            i = 1;
        }
        PrintWriter out = resp.getWriter();
        out.print(i);
        out.close();
    }

}
